package com.yazi.mxz.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.db.dao.RoleDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Role;
import com.yazi.mxz.common.service.RoleService;

@Service("roleService")
public class RoleServiceImpl extends BasicService<Role> implements RoleService {
	private static Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);

	@Resource
	RoleDao roleDao;

	@Override
	protected Dao<Role> getDao() {
		return roleDao;
	}
	
	@Override
	public Role addOrEdit(Role role) {
		Long roleId = role.getRoleId();
		if (null != roleId && roleId > 0) {
			log.debug("addOrModify->modify：roleId={}", roleId);
			Role destRole = roleDao.get(roleId);
			destRole = ValueFactory.combine(role, destRole);
			roleDao.merge(destRole);
			return destRole;
		} else {
			log.debug("addOrModify->add：name={}", role.getName());
			ValueFactory.validate(role);
			role.setRoleId(null);
			roleDao.persist(role);
			return role;
		}
	}

	@Override
	public List<Role> listByAdminId(long adminId) {
		log.debug("listByAdminId：adminId={}", adminId);
		return roleDao.listByAdminId(adminId);
	}

	@Override
	public List<Role> listByMemberId(long memberId) {
		log.debug("listByMemberId：memberId={}", memberId);
		return roleDao.listByMemberId(memberId);
	}

}
